package javass;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Fangfa {
	/**
	 * 插入
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public void inser(Userinfo ui ) throws SQLException, ClassNotFoundException{
		ConnectionUtil connutil=new ConnectionUtil();
		Connection conn=connutil.getConn();
		PreparedStatementUtil psu=new PreparedStatementUtil();
		String sql=" INSERT INTO userinfo(username,`password`,email)VALUES(?,?,?); ";
		PreparedStatement ps=psu.getPreparedStatement(conn, sql);
		ps.setString(1,ui.getUsername());
		ps.setString(2,ui.getPassword() );
		ps.setString(3, ui.getEmail());
		ps.executeUpdate();
		conn.close();
		ps.close();
	}
	public String login(Userinfo ui) throws ClassNotFoundException, SQLException{
		String str="0";
		ConnectionUtil connutil=new ConnectionUtil();
		Connection conn=connutil.getConn();
		PreparedStatementUtil psu=new PreparedStatementUtil();
		ResultSetUtil rsu=new ResultSetUtil();
		String sql=" SELECT u.`password`FROM userinfo u WHERE u.username=?;";
		PreparedStatement ps=psu.getPreparedStatement(conn, sql);
		ps.setString(1,ui.getUsername());
		ResultSet rs=rsu.getResultSet(ps);
		if(rs.next()){
			if (rs.getString(1).equals(ui.getPassword())) {
				str="1";
			}else{
				str="0";
			}
		}else{
			str="0";
		}
		conn.close();
		ps.close();
		return str;
	}

}
